<?php 
namespace Admin\Controller;
/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class TuigController extends AdminController {
	
	public function __construct (){
		parent::__construct();
		if(I('time_start') && I('time_end')){
            $begin       = I('time_start');
            $end         = I('time_end');
            //$pdkssj      = strtotime($begin)+31*24*3600; 
            //$pdjssj      = strtotime($end);
            //if($pdjssj >= $pdkssj){
            //    $this->error('只能查询一个月区间的数据！！');
            //}
            $this->assign('begin',$begin);
            $this->assign('end',$end);
        }else{
            $shijian   = time()-30*24*3600; //减30天
            $begin     = date('Y-m-d',$shijian);  //当前时间
            $end       = date('Y-m-d',time()); //减30天转换
			$this->assign('begin',$begin);
            $this->assign('end',$end);
        }
	}
	
	
	//费用统计
	public function index(){
		if(I('time_start') && I('time_end')){
			$begin   = I('time_start');
			$end     = I('time_end');
		}else{
			$begin   = time();
			$end     = time()-30*24*3600;
		}
		$data = D('Tuig')->select();

		foreach ($data as $key => $v) {
			
				$zj['baidu']   +=  $v['baidu'];
				$zj['so']      +=  $v['so'];
				$zj['google']  +=  $v['google'];
				$zj['sogou']   +=  $v['sogou'];
				$zj['dazhong'] +=  $v['dazhong'];
				$zj['weixin']  +=  $v['weixin'];
				$zj['weibo']   +=  $v['weibo'];
				$zj['tianmao'] +=  $v['tianmao'];
				$zj['smtg']    +=  $v['smtg'];
				$zj['laoguketj'] +=  $v['laoguketj'];
				$zj['yuangongtj'] +=  $v['yuangongtj'];
				$zj['gdt'] +=  $v['gdt'];
				$zj['my'] +=  $v['my'];
				$zj['hlj'] +=  $v['hlj'];
				$zj['qt'] +=  $v['qt'];
				$zj['xhs'] +=  $v['xhs'];
		}
		$this->assign('zj',$zj);
		$this->assign('_list',$data);
		$this->display();
	}
	
	//费用添加
	public function add(){
		$tuig  = M('Tuig');
		if(IS_POST){
			$data         = $tuig->create();
			$data['time'] = strtotime($data['time']); 
			$res  = $tuig->add($data);
			
			if($res){
				$this->success('添加成功！',U('Tuig/index'));  
			}else{
				$this->error('添加失败！');
			}
		}
		$this->display();
	}
	
	//费用修改
	public function edit(){
		$tuig  = M('Tuig');
		$id    = I('id');
		$tuiguang = $tuig->where(array('id'=>$id))->find();
		if(IS_POST){
			$data         = $tuig->create();
			$data['time'] = strtotime($data['time']); 
			$res  = $tuig->where(array('id'=>$data['id']))->save($data);
			if($res){
				$this->success('更新成功！',U('Tuig/index'));  
			}else{
				$this->error('添加失败！');
			}
		}
		$this->assign('data',$tuiguang);
		$this->display('add');
	}
	
	//费用删除
	public function del(){
		$tuig  = M('Tuig');
		$id    = I('id');
		$res   =  $tuig->where(array('id'=>$id))->delete();
		if($res){
			$this->success('删除成功',U('Tuig/index'));  
		}else{
			$this->error('添加失败！');
		}
		
	}
	//查询
	public function map($begin,$end,$mm){
		if($mm == '32'){
			$map['xsjd']       =  array('eq',$uid);
		}
		return $data;
	}
	//任务统计
	public function mubiao(){
		if(I('time_start') && I('time_end')){
			$begin   = I('time_start');
			$end     = I('time_end');
		}else{
			$begin   = time();
			$end     = time()-30*24*3600;
		}
		$data = D('renwu')->select();
		
		foreach ($data as $key => $v) {
			
				$zj['huoqu']   +=  $v['huoqu'];
				$zj['hunsha']  +=  $v['hunsha'];
				$zj['genpai']  +=  $v['genpai'];
				$zj['uv']      +=  $v['uv'];
				$zj['zongji']  +=  $v['zongji'];
		}
		$this->assign('zj',$zj);
		$this->assign('_list',$data);
		$this->display();
	}
	
	//任务添加
	public function renwuadd(){
		$renwu  = M('Renwu');
		if(IS_POST){
			$data         = $renwu->create();
			$data['time'] = strtotime($data['time']); 
			$res  = $renwu->add($data);
			
			if($res){
				$this->success('添加成功！',U('Tuig/mubiao'));  
			}else{
				$this->error('添加失败！');
			}
		}
		$this->display();
	}
	
	//任务修改
	public function renwuedit(){
		$renwu  = M('Renwu');
		$id    = I('id');
		$tuiguang = $renwu->where(array('id'=>$id))->find();
		if(IS_POST){
			$data         = $renwu->create();
			$data['time'] = strtotime($data['time']); 
			$res  = $renwu->where(array('id'=>$data['id']))->save($data);
			if($res){
				$this->success('更新成功！',U('Tuig/mubiao'));  
			}else{
				$this->error('添加失败！');
			}
		}
		$this->assign('data',$tuiguang);
		$this->display('renwuadd');
	}
	
	
	
}